package kr.hyunist.musicplayproject;

import java.util.Locale;

import kr.hyunist.musicplayproject.Fragment.AlbumListFragment;
import kr.hyunist.musicplayproject.Fragment.AllListFragment;
import kr.hyunist.musicplayproject.Fragment.FolderListFragment;
import kr.hyunist.musicplayproject.Fragment.SingerListFragment;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends FragmentActivity {

	SectionsPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				mSectionsPagerAdapter.getItem(position);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		
		return super.onOptionsItemSelected(item);
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		private Fragment allListFragment, albumListFragment, singerListFragment, folderListFragment;
		
		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
			allListFragment = new AllListFragment();
			albumListFragment = new AlbumListFragment();
			singerListFragment	= new SingerListFragment();
			folderListFragment = new FolderListFragment();
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = null;
			
			switch(position){
			case 0:
				return fragment = allListFragment;
			case 1: 
				return fragment = albumListFragment;
			case 2: 
				return fragment = singerListFragment;
			case 3: 
				return fragment = folderListFragment;
			}
			return fragment;
			
		}

		@Override
		public int getCount() {
			return 4;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase(l);
			case 1:
				return getString(R.string.title_section2).toUpperCase(l);
			case 2:
				return getString(R.string.title_section3).toUpperCase(l);
			case 3:
				return getString(R.string.title_section4).toUpperCase(l);
			}
			return null;
		}
	}

	

}
